package practice_list;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-03-21
 * Time:13:41
 */
public class 旋转链表 {
    public ListNode rotateRight(ListNode head, int k) {
        if (k == 0 || head == null || head.next == null) {
            return head;
        }
        int len = 1;
        ListNode iter = head;
        while (iter.next != null) {
            iter = iter.next;
            len++;
        }
        int n = len - k % len;
        if (n == len) {
            return head;
        }
        iter.next = head;
        while (n != 0) {
            iter = iter.next;
            n--;
        }
        ListNode ret = iter.next;
        iter.next = null;
        return ret;
    }
}
